/*BEGIN_LEGAL 
Intel Open Source License 

Copyright (c) 2002-2005 Intel Corporation 
All rights reserved. 
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.  Redistributions
in binary form must reproduce the above copyright notice, this list of
conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.  Neither the name of
the Intel Corporation nor the names of its contributors may be used to
endorse or promote products derived from this software without
specific prior written permission.
 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
END_LEGAL */

//Bugs and criticisms can be directed to
//myers.albert@gmail.com

//mcfTrace.h
//contains instruction analysis functions injected with mcfTrace.cpp

/*--------------------------*/
/* ASCII analysis routines  */ 
/*--------------------------*/

//Unconditional Direct Branches
VOID Emit_UnconditionalDirect_ASCII(const THREADID threadid, const ADDRINT address, const ADDRINT target)
{ 
   if( !CanEmit(threadid) ) return;
 
   IncrementBranchStatistics(UnconditionalDirectTaken);

   //Always taken 
   std::string type = ", U, D, T"; 
   //Descriptor String
   std::ostringstream convert;
   UINT8 assignedTID = *static_cast<UINT8*>(PIN_GetThreadData(tls_key, threadid));
   convert << std::dec << static_cast<UINT32>(assignedTID)
           << ", 0x" << std::hex << std::setw(ADDRWID) << std::setfill('0') << address 
           << ", 0x" << std::hex << std::setw(ADDRWID) << std::setfill('0') << target
           << type << std::endl;
   std::string ASCIIDescriptor = convert.str();
 
   PIN_GetLock(&table_lock, threadid+1);
   //Push back, will write on close
   ASCIIDescriptorTable.push_back(ASCIIDescriptor);
   //increment file counter
   IncrementFileCount(ASCIIDescriptor.size());
   PIN_ReleaseLock(&table_lock);
}
//Conditional Direct Branches
VOID Emit_ConditionalDirect_ASCII(const THREADID threadid, const ADDRINT address, const ADDRINT target, const BOOL taken)
{
   if( !CanEmit(threadid) ) return;
   
   std::string type;
   //Direct conditional, check if taken
   if(taken == 0)
   {
      IncrementBranchStatistics(ConditionalDirectNotTaken);
      type = ", C, D, NT";
   }
   else
   {
      IncrementBranchStatistics(ConditionalDirectTaken);
      type = ", C, D, T";
   }
   std::ostringstream convert;
   UINT8 assignedTID = *static_cast<UINT8*>(PIN_GetThreadData(tls_key, threadid));
   convert << std::dec << static_cast<UINT32>(assignedTID)
           << ", 0x" << std::hex << std::setw(ADDRWID) << std::setfill('0') << address 
           << ", 0x" << std::hex << std::setw(ADDRWID) << std::setfill('0') << target 
           << type << std::endl;
   std::string ASCIIDescriptor = convert.str();
 
   PIN_GetLock(&table_lock, threadid+1);
   //Push back, will write on close
   ASCIIDescriptorTable.push_back(ASCIIDescriptor);
   //increment file counter
   IncrementFileCount(ASCIIDescriptor.size());
   PIN_ReleaseLock(&table_lock);
}
//Unconditional Direct Branches
VOID Emit_UnconditionalIndirect_ASCII(const THREADID threadid, const ADDRINT address, const ADDRINT target)
{
   if( !CanEmit(threadid) ) return;

   IncrementBranchStatistics(UnconditionalIndirectTaken);

   std::string type = ", U, I, T"; 
   std::ostringstream convert;
   UINT8 assignedTID = *static_cast<UINT8*>(PIN_GetThreadData(tls_key, threadid));
   convert << std::dec << static_cast<UINT32>(assignedTID)
           << ", 0x" << std::hex << std::setw(ADDRWID) << std::setfill('0') << address 
           << ", 0x" << std::hex << std::setw(ADDRWID) << std::setfill('0') << target 
           << type << std::endl;
   std::string ASCIIDescriptor = convert.str();
 
   PIN_GetLock(&table_lock, threadid+1);
   //Push back, will write on close
   ASCIIDescriptorTable.push_back(ASCIIDescriptor);
   //increment file counter
   IncrementFileCount(ASCIIDescriptor.size());
   PIN_ReleaseLock(&table_lock);
}
/*--------------------------*/
/* ASCII with Disassembly analysis routines  */ 
/*--------------------------*/

//Unconditional Direct Branches
VOID Emit_UnconditionalDirect_ASCII_Dis(const THREADID threadid, const ADDRINT address, const ADDRINT target)
{ 
   if( !CanEmit(threadid) ) return;

   IncrementBranchStatistics(UnconditionalDirectTaken);

   //Disassembly
   std::string ins;
   getAssemblyString(ins, threadid, address);
 
   //Always taken 
   std::string type = ", U, D, T"; 
   //Descriptor String
   ostringstream convert;
   UINT8 assignedTID = *static_cast<UINT8*>(PIN_GetThreadData(tls_key, threadid));
   convert << std::dec << static_cast<UINT32>(assignedTID)
           << ", 0x" << std::hex << std::setw(ADDRWID) << std::setfill('0') << address 
           << ", 0x" << std::hex << std::setw(ADDRWID) << std::setfill('0') << target 
           << type 
           << "\t" << ins << std::endl;
   std::string ASCIIDescriptor = convert.str();
 
   PIN_GetLock(&table_lock, threadid+1);
   //Push back, will write on close
   ASCIIDescriptorTable.push_back(ASCIIDescriptor);
   //increment file counter
   IncrementFileCount(ASCIIDescriptor.size());
   PIN_ReleaseLock(&table_lock);
}

//Conditional Direct Branches
VOID Emit_ConditionalDirect_ASCII_Dis(const THREADID threadid, const ADDRINT address, const ADDRINT target, const BOOL taken)
{
   if( !CanEmit(threadid) ) return;

   //Disassembly
   std::string ins;
   getAssemblyString(ins, threadid, address);

   //Direct conditional so check if taken
   std::string type;
   if(taken == 0)
   {
      type = ", C, D, NT";
      IncrementBranchStatistics(ConditionalDirectNotTaken);
   }
   else
   {
      IncrementBranchStatistics(ConditionalDirectTaken);
      type = ", C, D, T";
   }
   std::ostringstream convert;
   UINT8 assignedTID = *static_cast<UINT8*>(PIN_GetThreadData(tls_key, threadid));
   convert << std::dec << static_cast<UINT32>(assignedTID)
           << ", 0x" << std::hex << std::setw(ADDRWID) << std::setfill('0') << address 
           << ", 0x" << std::hex << std::setw(ADDRWID) << std::setfill('0') << target 
           << type 
           << "\t" << ins << std::endl;
   std::string ASCIIDescriptor = convert.str();
 
   PIN_GetLock(&table_lock, threadid+1);
   //Push back, will write on close
   ASCIIDescriptorTable.push_back(ASCIIDescriptor);
   //increment file counter
   IncrementFileCount(ASCIIDescriptor.size());
   PIN_ReleaseLock(&table_lock);
}
//Unconditional Indirect Branches
VOID Emit_UnconditionalIndirect_ASCII_Dis(const THREADID threadid, const ADDRINT address, const ADDRINT target)
{
   if( !CanEmit(threadid) ) return;

   IncrementBranchStatistics(UnconditionalIndirectTaken);

   //Disassembly
   std::string ins;
   getAssemblyString(ins, threadid, address);
   
   std::string type = ", U, I, T"; 
   std::ostringstream convert;
   UINT8 assignedTID = *static_cast<UINT8*>(PIN_GetThreadData(tls_key, threadid));
   convert << std::dec << static_cast<UINT32>(assignedTID)
           << ", 0x" << std::hex << std::setw(ADDRWID) << std::setfill('0') << address 
           << ", 0x" << std::hex << std::setw(ADDRWID) << std::setfill('0') << target
           << type << "\t" << ins << std::endl;
   std::string ASCIIDescriptor = convert.str();
 
   //critical section
   PIN_GetLock(&table_lock, threadid+1);
   //push back, will write on close
   ASCIIDescriptorTable.push_back(ASCIIDescriptor);
   //increment file counter
   IncrementFileCount(ASCIIDescriptor.size());
   PIN_ReleaseLock(&table_lock);
}

/*--------------------------*/
/* Binary analysis routines  */
/*--------------------------*/

//Unconditional Direct Branches
VOID Emit_UnconditionalDirect_Bin(const THREADID threadid,const ADDRINT address,const ADDRINT target)
{
   if( !CanEmit(threadid) ) return;

   IncrementBranchStatistics(UnconditionalDirectTaken);

   //setup descriptor
   BinaryDescriptorTableEntry binDescriptor;
   binDescriptor.tid = *static_cast<UINT8*>(PIN_GetThreadData(tls_key, threadid));
   binDescriptor.branchAddress = address;
   binDescriptor.targetAddress = target;
   binDescriptor.branchType = UnconditionalDirectTaken;

   //critical section
   PIN_GetLock(&table_lock, threadid+1);
   binDescriptorTable.push_back(binDescriptor);
   //increment file counter
   IncrementFileCount(BinDescriptorTableEntrySize);
   PIN_ReleaseLock(&table_lock);
}
VOID Emit_ConditionalDirect_Bin(const THREADID threadid,const ADDRINT address,const ADDRINT target,const BOOL taken)
{
   if( !CanEmit(threadid) ) return;

   //setup descriptor
   BinaryDescriptorTableEntry binDescriptor;
   binDescriptor.tid = *static_cast<UINT8*>(PIN_GetThreadData(tls_key, threadid));
   binDescriptor.branchAddress = address;
   binDescriptor.targetAddress = target;
   //If taken paramater will be non-zero 
   if(taken == 0)
   {
      IncrementBranchStatistics(ConditionalDirectNotTaken);
      binDescriptor.branchType = ConditionalDirectNotTaken;
   }
   else
   {
      IncrementBranchStatistics(ConditionalDirectTaken);
      binDescriptor.branchType = ConditionalDirectTaken;
   }

   //critical section
   PIN_GetLock(&table_lock, threadid+1);
   binDescriptorTable.push_back(binDescriptor);
   //increment file counter
   IncrementFileCount(BinDescriptorTableEntrySize);
   PIN_ReleaseLock(&table_lock);
}

VOID Emit_UnconditionalIndirect_Bin(const THREADID threadid,const ADDRINT address,const ADDRINT target)
{
   if( !CanEmit(threadid) ) return;

   IncrementBranchStatistics(UnconditionalIndirectTaken);

   //setup descriptor  
   BinaryDescriptorTableEntry binDescriptor;
   binDescriptor.tid = *static_cast<UINT8*>(PIN_GetThreadData(tls_key, threadid));
   binDescriptor.branchAddress = address;
   binDescriptor.targetAddress = target;
   binDescriptor.branchType = UnconditionalIndirectTaken;
   
   //critical section
   PIN_GetLock(&table_lock, threadid+1);
   binDescriptorTable.push_back(binDescriptor);
   //increment file counter
   IncrementFileCount(BinDescriptorTableEntrySize);
   PIN_ReleaseLock(&table_lock);
}

/*--------------------------*/
/* Other analysis routines  */
/*--------------------------*/

//used to decrement fastfoward_count and trace length for all instructions
VOID SetFastForwardAndLength(const THREADID threadid)
{
   //critical section
   PIN_GetLock(&count_lock, threadid+1);
   //if file limit is reacher or trace limit is reached
   //exit tool
   if( (traceLength + 1) == Length.Value() && !noLength )
   {  
      PIN_Detach();
      PIN_ReleaseLock(&count_lock);
      return;
   } 
   //decrement fast forward count   
   if( fastforwardLength < Skip.Value())
      fastforwardLength++;
    
   //otherwise increment length
   else
      traceLength++;  
   
   PIN_ReleaseLock(&count_lock);
   return;
}

//called on signals
VOID Sig(THREADID threadid, CONTEXT_CHANGE_REASON reason, const CONTEXT *ctxtFrom, CONTEXT *ctxtTo, INT32 sig, VOID *v)
{
   if( !CanEmit(threadid) ) return;

   IncrementBranchStatistics(UnconditionalIndirectTaken);

   //From the Pin user guide
   //1. If the tool acquires any locks from within a Pin call-back, it must release those locks before returning from that call-back. 
   //Holding a lock across Pin call-backs violates the hierarchy with respect to the Pin internal locks.
   //
   //2. If the tool calls a Pin API from within a Pin call-back or analysis routine, it should not hold any tool locks when calling the 
   //API. Some of the Pin APIs use the internal Pin locks so holding a tool lock before invoking these APIs violates the hierarchy with 
   //respect to the Pin internal locks.    
   PIN_GetLock(&cout_lock, threadid+1);
   //get local id
   UINT8 assignedTID = *static_cast<UINT8*>(PIN_GetThreadData(tls_key, threadid));
   std::cout << "mcfTrace: Thread " << static_cast<UINT16>(assignedTID)  << " received signal " << sig << std::endl;
   if(reason == CONTEXT_CHANGE_REASON_FATALSIGNAL)
   {
      std::cout << "mcfTrace: Fatal signal, exiting immediately..." << std::endl;
      PIN_GetLock(&table_lock, threadid+1);
      closeOutput();
      PIN_ReleaseLock(&table_lock);
      return;
   }
   PIN_ReleaseLock(&cout_lock);

   //get context
   ADDRINT from = PIN_GetContextReg(ctxtFrom, REG_INST_PTR);
   ADDRINT to = PIN_GetContextReg(ctxtTo, REG_INST_PTR);

   //Exception handling and returns from exception handlers is an indirect branch 
   if( ASCII.Value() )
   {
       
      std::ostringstream convert;
      convert << std::dec << static_cast<UINT16>(assignedTID)
                     << ", 0x" << std::hex << std::setw(ADDRWID) << std::setfill('0') << from 
                     << ", 0x" << std::hex << std::setw(ADDRWID) << std::setfill('0') << to 
                     << std::dec << ", U, I, T";

      if( AnnotateDisassembly.Value() )
      {
         std::string ins;
         getAssemblyString(ins, threadid, from);

         if(reason==CONTEXT_CHANGE_REASON_SIGNAL)
               ins = "\tSignal At: " + ins;
         else if(reason==CONTEXT_CHANGE_REASON_SIGRETURN)
               ins = "\tReturning from signal handler: " + ins;
         convert << ins;
      }
      convert << std::endl;
      std::string ASCIIDescriptor = convert.str();

      PIN_GetLock(&table_lock, threadid+1);
      ASCIIDescriptorTable.push_back(ASCIIDescriptor);
      //increment file counter
      IncrementFileCount(ASCIIDescriptor.size());
      PIN_ReleaseLock(&table_lock);    
   }
   else
   {
      BinaryDescriptorTableEntry binDescriptor;
      binDescriptor.tid = assignedTID;
      binDescriptor.branchAddress = from;
      binDescriptor.targetAddress = to;
      binDescriptor.branchType = UnconditionalIndirectTaken;

      //critical section
      PIN_GetLock(&table_lock, threadid+1);
      binDescriptorTable.push_back(binDescriptor);       
      //increment file counter
      IncrementFileCount(BinDescriptorTableEntrySize);
      PIN_ReleaseLock(&table_lock);
   }
}
